home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
vmed.arc
/
ED3.CCC
< prev
next >
Wrap
Text File
|
1985-12-03
|
15KB
|
715 lines
/* Screen editor: command mode commands
*
* Module: ed3/ccc
* Date: November 12, 1983
* Changed: February 18, 1984 (corrected LIST cmd, added
* loading and saving mode messages)
* Changed: February 29, 1984 (added VMED.002 stuff)
* Changed: March 8, 1984 (bufatbot() refs)
*/
#include ed0
/* data global to this module */
#option zvar ON
int pastefile;
#option zvar OFF
char filename[SYSFNMAX],
patxx[MAXLEN1]; /* reserve space for EOS */
/* append command.
* load a file into main buffer at current location.
* this command does NOT change the current file name
*/
append(args) char *args;
{ char buffer[MAXLEN], /* disk line buffer */
locfn[SYSFNMAX]; /* local file name */
int n,topline;
FILE *file;
/* get file name which follows command */
if (name1(args,locfn)==ERR)
return;
if (locfn[0]==EOS) {
outm(noarg);
return;
}
/* open new file */
pmtmode("append:");
if ((file=sysopen(locfn,"r"))==ERR) {
outm(filenot);
return;
}
/* read the file into the buffer */
for (; (n=readline(file,buffer,MAXLEN)) >= 0; pmtlnx())
if(store(buffer,n) == ERR)
break;
/* close the file */
sysclose(file);
/* redraw the screen so topline will be at top
* of the screen after command() does a CR/LF.
*/
topline=max(1,bufln()-SCRNL2);
bufout(topline,2,SCRNL2);
bufgo(topline);
}
/* clear main buffer and file name */
clear()
{ /* make sure it is ok to clear buffer */
if (chkbuf()!=YES)
return;
pmtfile("");
filename[0]=0;
outclr();
outxy(0,SCRNL1);
bufnew();
}
/* multiple line delete command */
delete(args) char *args;
{ int from,to;
/* make sure there ARE arguments */
default(args,".,.");
if (get2args(args,&from,&to)==ERR || from>to)
return;
/* go to first line to be deleted */
if (bufgo(from)==ERR)
return;
/* delete all lines between from and to */
if (bufdeln(to-from+1)==ERR)
return;
/* redraw the screen */
bufout(bufln(),1,SCRNL1);
}
/* list lines to list device */
list(args) char *args;
{ char linebuf[MAXLEN1],temp;
int n,from,to,oldline;
/* save the buffer's current line */
oldline=bufln();
/* get starting, ending lines to print */
if (get2args(args,&from,&to)==ERR)
return;
to = min(to,buffree());
/* print lines one at a time to list device */
while (in_txt() && from<=to) {
/* check for interrupt */
if (chkkey()==YES)
break;
/* print line to list device */
if (bufgo(from++) || past_eof()) /* 3-8-84 */
break;
n = bufgetln(linebuf,MAXLEN1);
n=min(n,MAXLEN);
linebuf[n]=CR;
fmtsout(linebuf,0);
fmtcrlf();
}
/* restore cursor */
bufgo(oldline);
}
/* display a line number and line */
dspline(linebuf) int linebuf;
{ putdec(bufln(),0);
fmtsout(":",outgety());
fmtsout(linebuf,8);
outdeol();
fmtcrlf();
}
/* load file into buffer */
load(args) char *args;
{ char buffer[MAXLEN], /* disk line buffer */
locfn[SYSFNMAX];
int n,topline;
FILE *file;
/* get filename following command */
if (name1(args,locfn)==ERR)
return(NO);
if (locfn[0]==EOS) {
outm(noarg);
return(NO);
}
/* give user a chance to save the buffer */
if (chkbuf()==NO)
return(NO);
/* open the new file */
pmtmode("loading:");
if ((file=sysopen(locfn,"r"))==ERR) {
outm(filenot);
return(NO);
}
/* update file name */
syscopfn(locfn,filename);
/* clear the buffer */
bufnew();
pmtfile(filename);
/* read the file into the buffer */
for (; (n=readline(file,buffer,MAXLEN)) >= 0; pmtlnx())
if(store(buffer,n)==ERR)
break;
/* close the file */
sysclose(file);
/* indicate that the buffer is fresh */
bufsaved();
/* set current lin to line 1 */
bufgo(1);
/* redraw the screen so that topline will be on line 1
* after command() does a CR/LF.
*/
topline=max(1,bufln()-SCRNL2);
bufgo(topline);
return(YES);
}
/* change current file name */
name(args) char *args;
{ name1(args,filename);
pmtfile(filename);
}
/* check syntax of args.
* copy to filename.
* return OK if the name is valid.
*/
name1(args,filename) char *args,*filename;
{ /* skip command */
args=skiparg(args);
args=skipbl(args);
/* check file name syntax */
if (syschkfn(args)==ERR)
return(ERR);
/* copy filename */
syscopfn(args,filename);
return(OK);
}
/* check the length of a line */
chklen(n) char n;
{ if (n>MAXLEN) {
outm(linetrun);
n=MAXLEN;
}
return(n);
}
/* store a line in the buffer */
store(buff,count) int buff; char count;
{ count = chklen(count);
if (bufins(buff,count)==ERR)
return(ERR);
if (bufdn()==ERR)
return(ERR);
return(OK);
}
/* set tab stops for fmt routines */
tabs(args) char *args;
{ int n;
if (number(skipbl(skiparg(args)),&n) == NO)
return;
fmtset(n);
}
/* return YES if buffer may be drastically changed */
chkbuf()
{ if (bufchng()==NO)
return(YES); /* buffer unchanged. no problem */
fmtsout("buffer not saved. proceed ? ",0);
pmtline();
if (tolower(syscout(syscin()))!='y') {
fmtcrlf();
outm(cancel);
return(NO);
}
/*else*/
fmtcrlf();
return(YES);
}
/* get two arguments from the argument line 'args'.
* no arguments imply 1 HUGE.
* one argument implies both args the same.
*/
get2args(args,val1,val2) char *args; int *val1,*val2;
{ /* skip over the command */
args=skiparg(args);
args=skipbl(args);
if (*args==EOS) {
*val1=1;
*val2=buffree(); /* 3-7-84 */
return(OK);
}
/* check first argument */
if (number(args,val1)==NO) {
outm(badarg);
return(ERR);
}
/* skip over first argument */
args=skiparg(args);
args=skipbl(args);
/* 1 arg; arg 2 is same */
if (*args==EOS) {
*val2=*val1;
return(OK);
}
/* check second argument */
if (number(args,val2)==NO) {
outm(badarg);
return(ERR);
}
else return(OK);
}
/* skip over all except EOS, commas, and blanks */
skiparg(args) char *args;
{ while (*args!=EOS && *args!=' ' && *args!=',')
++args;
return(args);
}
/* skip over all blanks and commas */
skipbl(args) char *args;
{ while (*args==' ' || *args==',')
++args;
return(args);
}
/* global change command */
change(args) char *args;
{ char oldline[MAXLEN1],
newline[MAXLEN1],
oldpat[MAXLEN1],
newpat[MAXLEN1],
flag;
int from,to,col,n,k,temp;
if (get2args(args,&from,&to)==ERR)
return;
flag = 1;
/* get search and change masks into oldpat, newpat */
outm(smask);
getcmnd(oldpat,15);
fmtcrlf();
if (oldpat[0]==EOS)
return;
pmtline();
fmtsout("change mask ? ",0);
getcmnd(newpat,15);
fmtcrlf();
temp = bufln();
to = min(to,buffree());
/* make substitution for lines between from, to */
while (from <= to) {
if (chkkey() || bufgo(from++)==EOF)
break; /* 3-8-84 */
n=bufgetln(oldline,MAXLEN);
n=min(n,MAXLEN);
oldline[n]=EOS;
/* '^' anchors search */
if (oldpat[0]=='^')
if (amatch(oldline,oldpat+1,0)==YES) {
k=replace(oldline,newline,oldpat+1,newpat,0);
if (k==ERR)
return;
flag = 0;
dspline(newline);
bufrepl(newline,k);
continue;
}
/* search oldline for oldpat */
col=0;
while (col<n)
if (amatch(oldline,oldpat,col++)==YES) {
k=replace(oldline,newline,oldpat,newpat,col-1);
if(k==ERR)
return;
flag = 0;
dspline(newline);
bufrepl(newline,k);
break;
}
pmtmode("changing:");
}
bufgo(temp);
if(flag)
outm(patnotfnd);
}
/* search all lines below the current line for a pattern
* return -1 if pattern not found
* otherwise, return column number of start of pattern
*/
find()
{ return(search1(bufln()+1,buffree(),YES)); }
/* global search command */
search(args) char *args;
{ int from,to;
if (get2args(args,&from,&to)==ERR)
return;
search1(from,to,NO);
}
/* search lines for a pattern.
* if flag == YES: stop at the first match.
* return -1 if no match.
* otherwise return column number of match.
* if flag == NO: print all matches found.
*/
search1(from,to,flag) int from,to; char flag;
{ char line[MAXLEN1];
int col,n;
/* get search mask into pat */
outm(smask);
getcmnd(patxx,15);
fmtcrlf();
if (patxx[0]==EOS)
return;
to = min(to,buffree());
pmtmode("searching:");
/* search all lines between from and to for pat */
while (from<=to) {
if (chkkey()==YES ||
bufgo(from++)==EOF)
break;
n=bufgetln(line,MAXLEN);
n=min(n,MAXLEN);
line[n]=EOS;
pmtlnx();
/* ^ anchors search */
if (patxx[0]=='^') {
if (amatch(line,patxx+1,0)==YES) {
if (flag==NO)
dspline(line);
else return(0);
}
continue;
}
/* search whole line for match */
col=0;
while (col<n)
if (amatch(line,patxx,col++)==YES)
if (flag==NO) {
dspline(line);
break;
}
else return(col-1);
}
/* all searching is finished */
if (flag==YES)
return(-1);
else fmtcrlf();
}
/* search for next occurance of pattern from edit mode */
srch1()
{ char line[MAXLEN1];
int col,n,old,from,to;
old = bufln();
from = old + 1;
to = buffree();
pmtmode("searching:");
/* search all lines between from and to for pat */
while (from <= to) { /* 6-17-84 */
if (bufgo(from++)==EOF)
break; /* 3-8-84 */
n=bufgetln(line,MAXLEN);
n=min(n,MAXLEN);
line[n]=EOS;
pmtlnx();
/* ^ anchors search */
if (patxx[0]=='^') {
if (amatch(line,patxx+1,0)==YES) {
edgo(bufln(),0);
return;
}
continue;
}
/* search whole line for match */
col=0;
while (col<n)
if (amatch(line,patxx,col++)==YES) {
edgo(bufln(),col-1);
return;
}
}
bufgo(old);
}
/* return YES if the user has pressed any key.
* blanks cause a tranparent pause.
*/
chkkey()
{ char c;
c=syscstat();
if (c==0)
return(NO); /* no character at keyboard */
else if (syscin()==' ') {
/* pause. another blank ends pause */
pmtline();
while (!syscstat()) /* modified 3-7-84 */
;
if (syscin()==' ')
return(NO);
}
/* we got a nonblank character */
return(YES);
}
/* anchored search for pattern in text line at column col.
* return YES if the pattern starts at col.
*/
amatch(line,pat,col) char *line,*pat; int col;
{ int k;
k=0;
while(pat[k]!=EOS) {
if (pat[k]==line[col]) {
++k;
++col;
}
else if ((pat[k]=='?')&(line[col]!=EOS)) {
/* question mark matches any character */
++k;
++col;
}
else return(NO);
}
/* the entire pattern matches */
return(YES);
}
/* replace oldpat in oldline by newpat starting at col.
* put result in newline.
* return number of characters in newline.
*/
replace(oldline,newline,oldpat,newpat,col)
char *oldline,*newline,*oldpat,*newpat;
int col;
{ int k;
char *tail,*pat;
/* copy oldline preceding col to new line */
k=0;
while(k<col)
newline[k++]=*oldline++;
/* remember where end of oldpat in oldline is */
tail=oldline;
pat=oldpat;
while(*pat++!=EOS)
++tail;
/* copy newpat to newline.
* use oldline and oldpat to resolve question marks
* in newpat.
*/
while(*newpat!=EOS) {
if (k>MAXLEN-1) {
outm(nltolong);
return(ERR);
}
if (*newpat!='?') {
/* copy newpat to newline */
newline[k++]=*newpat++;
continue;
}
/* scan for '?' in oldpat */
while (*oldpat!='?') {
if (*oldpat==EOS) {
outm(toomany);
return(ERR);
}
++oldpat;
++oldline;
}
/* copy char from oldline to newline */
newline[k++]=*oldline++;
++oldpat;
++newpat;
}
/* copy oldine after oldpat to newline */
while(*tail!=EOS) {
if (k>=MAXLEN-1) {
outm(nltolong);
return(ERR);
}
newline[k++]=*tail++;
}
newline[k]=EOS;
return(k);
}
/* copy lines from one part of file to another
* source lines are deleted if flag == YES
*/
copy(args,flag)
char *args;
int flag;
{
int start,linend,to,new,diff;
/* Skip command */
args = skipbl(skiparg(args));
/* were there arguments? */
if (*args != EOS){
if (number(args,&start) == ERR) return;
if (get2args(args,&linend,&to) == ERR) return;
}
else { /* no, prompt for them */
start = getnum("Start");
linend = getnum("End");
to = getnum("To");
}
/* Check if "to" is within "start" to "linend" */
if ((start <= to) & (linend >= to)){ /* fixes bug from original code */
outm(flag ? badmove : badcopy);
return;
}
new = bufln();
if (writefile("VM/MOV","w",start,linend) == ERR) return;
if (bufgo(to) == ERR) return;
append(" VM/MOV");
diff = linend - start + 1;
/* if this was a "move", we need to delete the old lines
* otherwise, just restore the line counter to old line
*/
if (flag == YES){
if (start > to) start += diff;
if (bufgo(start) == ERR) return;
if (bufdeln(diff) == ERR) return;
}
else if (new > to) new += diff;
bufgo(new);
}
/* get number handler. get a
* line number from user
*/
getnum(ptr) char *ptr;
{ int numb;
char buf[MAXLEN1];
fmtsout(ptr,0);
fmtsout(" line: ",0);
getcmnd(buf,15); /* get the answer */
number(buf,&numb); /* copy the answer into 'numb' */
fmtcrlf();
return(numb);
}
/* paste from VM/PST to present position */
paste()
{
append(" VM/PST");
pastefile = 0;
}
/* save entire file */
save(args)
char *args;
{
pmtmode("saving:");
/* default to file in header line */
default(args,filename);
args = skipbl(skiparg(args));
if (writefile(args,"w",1,buffree()) != ERR)
bufsaved();
}
/* keep a portion of workfile to VM/PST */
keep(args)
char *args;
{
int start,linend;
char *mode;
mode = (pastefile ? "a" : "w");
/* default to current line only */
default(args,".,.");
if (get2args(args,&start,&linend) == ERR){
outm(badarg);
return;
}
pmtmode("writing:");
writefile("VM/PST",mode,start,linend);
pastefile = 1;
}
/* "put" a part of the working file to the named file */
putfile(args)
char *args;
{
int start,linend;
args = skipbl(skiparg(args));
if (*args == EOS){
outm(noarg);
return;
}
start = getnum("Start");
linend = getnum("End");
pmtmode("writing:");
writefile(args,"w",start,linend);
}
/* write the specified lines to the specified file */
writefile(args,mode,start,linend)
char *args,*mode;
int start,linend;
{
int oldline,n;
char linebuf[MAXLEN];
FILE *file;
/* keep from outputting beyond workfile's end */
linend = min(linend,buffree());
if (linend < start){
outm(backwards);
return(ERR);
}
else if (*args == EOS){
outm(noarg);
return(ERR);
}
else if ((file = sysopen(args,mode)) == ERR)
return(ERR);
/* remember old line number */
oldline = bufln();
/* write file */
if (bufgo(start) == ERR){
sysclose(file);
return(ERR);
}
while(start <= linend)
{
bufgo(start++);
n = bufgetln(linebuf,MAXLEN);
n = min(n,MAXLEN);
if (pushline(file,linebuf,n) != ERR) {
pmtlnx();
continue;
}
sysclose(file);
bufgo(oldline);
return(ERR);
}
/* close the file */
sysclose(file);
bufgo(oldline);
return(OK);
}
/* substitute default arguments if none given */
default(args,defltarg)
char *args,*defltarg;
{
args=skipbl(skiparg(args));
if (*args != EOS) return;
*args++ = ' ';
while(*defltarg != EOS){*args++ = *defltarg++;}
*args = EOS;
}
/* end module ed3/ccc */